package demo0;



/*
命令模式(Command)将一个请求封装为一个对象，从而使你可用不同的请求对客户进行参数化
对请求排队或记录请求日志，以及支持可撤销的操作
 */
public class Main {
    public static void main(String[] args) {
        Receiver r = new Receiver();
        Command  c = new ConcreteCommand(r);
        Invoker i = new Invoker();
        i.setCommand(c);
        i.ExecuteCommand();
    }
}

//Command 类，用来声明执行操作的接口
abstract class Command{
    protected Receiver receiver;
    public Command(Receiver receiver){
        this.receiver = receiver;
    }
    abstract public void Execute();
}

//ConcreteCommand 类将一个接收者对象绑定于一个动作，调用接收者相应的操作，以实现Execute

class  ConcreteCommand extends Command{
    public ConcreteCommand(Receiver receiver) {
        super(receiver);
    }

    @Override
    public void Execute() {
        receiver.Action();
    }
}

//Invoker 类 要求该命令执行这个请求。
class Invoker{
    private Command command;
    public void setCommand(Command command){
        this.command = command;
    }
    public void ExecuteCommand(){
        command.Execute();
    }
}

class Receiver{
    public void Action(){
        System.out.println("执行请求！");
    }
}